//
//  ViewController.swift
//  复制数组
//
//  Created by 崔博 on 2017/7/22.
//  Copyright © 2017年 崔博. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
       
        var people = [Person(name: "Bob", country: "China"),Person(name: "Jone", country: "France")]
        
        people[0].country = "UK"
        var otherPeople = people
        
        print("Country:\(otherPeople[0].country)")
    }

    // 数组的深拷贝 必须检出没个元素 然后继承NSObject 并实现NSCopying协议
    func deepCopy(data: [AnyObject]) -> [AnyObject] {
        return data.map({ (item) -> AnyObject in
            if (item is NSCopying || item is NSObject){
            return item (item as NSObject).copy()
            })else{
            return item;
        }
    }
}

class Person: NSObject, NSCopying {
    var name: String
    var country: String
    init(name: String, country: String) {
        self.name = name
        self.country = country
    }
    func copy(with zone: NSZone? = nil) -> Any {
        return Person(name: self.name, country: self.country)
    }
}
